package handle

import (
	"context"
	"fmt"
	"userSrv/model/entidy"
	"userSrv/model/menthod"
	"userSrv/proto/UserSrv"
)

type User struct {
}

func (u *User) UserLogin(ctx context.Context, req *UserSrv.UserLoginReq) (*UserSrv.UserLoginRes, error) {
	login, err := menthod.UserLogin(req.Mobile)
	if err != nil {
		return nil, fmt.Errorf("登录失败: %v", err)
	}
	if req.Password != login.Password {
		return nil, fmt.Errorf("账号或密码输入错误")
	}

	return &UserSrv.UserLoginRes{Id: int64(login.ID)}, nil
}

func (u *User) UserRegister(ctx context.Context, req *UserSrv.UserRegisterReq) (*UserSrv.UserRegisterRes, error) {
	fmt.Println(req)
	user := entidy.User{
		Mobile:   req.Mobile,
		Password: req.Password,
		NickName: req.NickName,
		Email:    req.Email,
	}
	register, err := menthod.UserRegister(user)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}
	return &UserSrv.UserRegisterRes{Id: int64(register.ID)}, nil
}
